<?php
	include_once( 'class.db.php' );
	
	function sendsinamessage($sinaid,$message="Hello Scutter",$username="2397055548@qq.com",$password="jiaowukexing"){
		include_once('./snoopy/snoopy.class.php');
        $snoopy = new Snoopy();
		$message = urlencode($message);
        $url = "http://weibopm.sinaapp.com/?uid=".$sinaid."&content=".$message;
		$snoopy->fetchtext($url);
	}
	
	function sendmail($to, $subject = "Hi!", $message = "Hello World!", $from = "s.cutter20xx@gmail.com", $pass = "jiaowukexing"){
		require_once("class.phpmailer.php");
		$Correo = new PHPMailer();
		$Correo->CharSet = "UTF-8";
		$Correo->IsSMTP();
		$Correo->SMTPAuth = true;
		$Correo->SMTPSecure = "tls";
		$Correo->Host = "smtp.gmail.com";
		$Correo->Port = 587;
		$Correo->Username = $from;;
		$Correo->Password = $pass;
		$Correo->SetFrom($from,'S-Cutter');
		$Correo->FromName = "S-Cutter";
		$Correo->AddAddress($to);
		$Correo->Subject = $subject;
		$Correo->Body = $message;
		$Correo->IsHTML (true);
		$k=0;$res = '';
		while($k<3&&!$res){
			$res = $Correo->Send();
			$k++;
		}
		if (!$res)
			return "0";
		else
			return "1";	
	}
	
	function setscoremessage($content,$type = "mail"){
		$content = mb_convert_encoding($content,"utf-8","gbk");
		$content = explode('=>',$content);
		switch($type){
			case 'mail':$text = '您好！<br><p><a href = "http://weibo.com/u/2597960902"><b> S-Cutter <b></a>教务辅助星 提醒您，您的最新考试结果如下：<br><br>';
						foreach($content as $line){
							$text .= ($line."<br>");
						}
						$text .= '</p><br><a href = "http://weibo.com/u/2597960902"><b> S-Cutter <b></a><br>';
						$text .= date("Y-m-d H:i:s");
						//$text = mb_convert_encoding($text,'utf-8','gbk');
						break;
			case 'sina':$text = "S-Cutter 教务辅助星温馨提示，您的最新考试结果如下：";
						foreach($content as $line){
							$text .= $line."; ";
						}
						break;
			default:	break;
		}
		return $text;
	}
	
	function setpkmessage($type = "mail"){
		switch($type){
			case 'mail':$text = '您好！<br><p><a href = "http://weibo.com/u/2597960902"><b> S-Cutter <b></a>教务辅助星 提醒您，您的评课操作已完成，请登录教务系统确认提交！如有意外，请通过 Email: s.cutter20xx@gmail.com 联系我们！ <br><br>';
						$text .= "感谢您对我们工作的支持！谢谢！";
						break;
			case 'sina':$text = "S-Cutter 教务辅助星温馨提示，您的评课操作已完成，请登录教务系统确认提交！";
						break;
			default:	break;
		}
		return $text;
	}
	
	function afterpked($jwuser){
		$db = new database();
		$query = "SELECT `email`,`sina_id` FROM `sina_jw2005` WHERE `jw_user`='$jwuser'";
		$result = $db->exec_sql($query);
		if(!$result[0][0]){
			$query = "SELECT `email` FROM `web_jw2005` WHERE `jw_user`='$jwuser'";
			$result = $db->exec_sql($query);
			if(!$result[0][0]) return 0;
		}
		$content = setpkmessage("mail");
		$res = sendmail($result[0][0],"S-Cutter 教务辅助星温馨提示",$content);
		if($result[0][1]){
			$content = setpkmessage("sina");
			sendsinamessage($result[0][1],$content);
		}
		if($res){
			$query = "UPDATE `sina_jw2005` SET `is_pk`='2' WHERE `jw_user`='$jwuser'";
			$result = $db->exec_sql($query,false);
			$query = "UPDATE `web_jw2005` SET `is_pk`='2' WHERE `jw_user`='$jwuser'";
			$result = $db->exec_sql($query,false);
		}
		return $res;
	}
	
	function afternewscore($jwuser,$content){
		$db = new database();
		$query = "SELECT `email`,`sina_id` FROM `sina_jw2005` WHERE `jw_user`='$jwuser'";
		$result = $db->exec_sql($query);
		if(!$result[0][0]){
			$query = "SELECT `email` FROM `web_jw2005` WHERE `jw_user`='$jwuser'";
			$result = $db->exec_sql($query);
			if(!$result[0][0]) return 0;
		}
		//$content = mb_convert_encoding($content,"gbk","utf-8");
		$text = setscoremessage($content,"mail");
		sendmail($result[0][0],"S-Cutter 教务辅助星温馨提示",$text);
		if($result[0][1]){
			$text = setscoremessage($content,"sina");
			sendsinamessage($result[0][1],$text);
		}
	}
	
	switch($_GET['cron']){
		case 'pk':	afterpked($_GET['jwuser']);
					break;
		case 'ts':	echo afternewscore($_GET['jwuser'],$_GET['content']);
					break;
		default:	break;
	}
	
?>
